﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;

namespace LibExtend
{
    public class AppConfig
    {
        private string Path = System.Windows.Forms.Application.StartupPath + @"\AppConfig.xml";

        private DataSet GetData()
        {
            if (!File.Exists(Path))
                return null;

            DataSet ds = new DataSet();
            ds.ReadXml(Path);
            return ds;
        }

        private void WriteNew(DataSet ds, string TableName, string FieldName, string Value)
        {
            DataTable DT = new DataTable(TableName);
            DT.Columns.Add(new DataColumn(FieldName));
            DataRow row = DT.NewRow();
            row[FieldName] = Value;
            DT.Rows.Add(row);

            ds.Tables.Add(DT);
            ds.WriteXml(Path);
        }

        public void WriteXml(string TableName,string FieldName,string Value)
        {
            DataSet ds = new DataSet("Data");
            ds = GetData();
            if (ds == null)
            {
                WriteNew(new DataSet("Data"), TableName, FieldName, Value);
            }
            else
            {
                if (ds.Tables.Contains(TableName))
                {
                    DataTable DT = ds.Tables[TableName];
                    if (DT == null)
                        WriteNew(ds, TableName, FieldName, Value);
                    else
                    {
                        if (!DT.Columns.Contains(FieldName))
                        {
                            DT.Columns.Add(new DataColumn(FieldName));
                            if (DT.Rows.Count == 0)
                            {
                                DataRow row = DT.NewRow();
                                row[FieldName] = Value;
                                DT.Rows.Add(row);
                            }
                            else
                                DT.Rows[0][FieldName] = Value;
                            ds.WriteXml(Path);
                        }
                        else
                        {
                            if (DT.Rows.Count == 0)
                            {
                                DataRow row = DT.NewRow();
                                row[FieldName] = Value;
                                DT.Rows.Add(row);
                                ds.WriteXml(Path);
                            }
                            else
                            {
                                DT.Rows[0][FieldName] = Value;
                                ds.WriteXml(Path);
                            }
                        }
                    }
                }
                else
                    WriteNew(ds, TableName, FieldName, Value);
            }
        }

        public string ReadXml(string TableName, string FieldName)
        {
            if (!File.Exists(Path))
            {
                return "";
            }
            else
            {
                DataSet ds = new DataSet();
                ds.ReadXml(Path);
                if (ds == null || ds.Tables.Count == 0 || !ds.Tables.Contains(TableName))
                    return "";
                else
                {
                    DataTable DT = ds.Tables[TableName];
                    if (DT == null || DT.Rows.Count == 0 || !DT.Columns.Contains(FieldName))
                        return "";
                    else
                    {
                        return DT.Rows[0][FieldName].ToString();
                    }
                }
            }
        }
    }
}
